
package com.lpf.transaction;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;

// 定义切面
// 注入Spring容器
@Slf4j
public class AopOfCust {

    @Autowired
    private TransactionUtils transactionUtils;

    private TransactionStatus begin;

    public void begin() {
        // log.error("......前置通知......");
    }

    public void after() {
        // log.error(".......后置通知......");
    }

    public void afterReturning() {
        // log.error(".......运行通知......");
    }

    public void afterThrowing() {
        log.error(".......异常通知......");
        transactionUtils.rollback(begin);
    }

    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        begin = transactionUtils.begin();
        log.error("环绕通知-前");

        // 放行 执行实际方法
        proceedingJoinPoint.proceed();  // 如果异常自己捕获了，没有抛出来到这里，下面的代码是会执行的

        log.error("环绕通知-后");
        transactionUtils.commit(begin);
    }

}
